package Spring;

import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * @program: planet-company
 * @description:
 * @author: jyz
 * @create: 2019-09-26 21:44
 **/
public class ProductTest2 {

    public static class Producter implements Runnable{
        private BlockingQueue queue;
        public Producter(BlockingQueue q){
            this.queue=q;
        }

        @Override
        public void run() {
            int sleep=0;
            for(int i=1;i<=100;i++){
                sleep = new Random().nextInt(100);
                try {
                    queue.put(i);
//                    Thread.sleep(sleep);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    }


    public static class Consumer implements Runnable{

        private BlockingQueue queue;

        public Consumer(BlockingQueue q){
            this.queue=q;
        }

        @Override
        public void run() {
            try {
                System.out.println(this.queue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }



    public static void main(String[] args){

        BlockingQueue queue = new ArrayBlockingQueue(100);
        //生产者生产消息
        for(int i=1;i<=100;i++){
            int sleep = new Random().nextInt(100);
            try {
                queue.put(i);
//                Thread.sleep(sleep);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        BlockingQueue b = new ArrayBlockingQueue(100);
        BlockingQueue c = new ArrayBlockingQueue(100);
        BlockingQueue d = new ArrayBlockingQueue(100);
        while (!queue.isEmpty()) {
            int number = 0;
            try {
                number = (int) queue.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            try {
                if (number % 3 == 0) {
                    b.put(number);
                } else if (number % 5 == 0) {
                    c.put(number);
                } else {
                    d.put(number);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }


        Thread B = new Thread(new Consumer(b));
        Thread C = new Thread(new Consumer(c));
        Thread D = new Thread(new Consumer(d));
        B.start();
        C.start();
        D.start();
    }



}
